import { useNavigate } from 'react-router-dom';
import { useAppDispatch, useAppSelector } from '../../redux/hooks';
import { setUserName } from '../../redux/slices/userSlice';
import { setCreator } from '../../redux/slices/gameSlice';

export default function useCreateUser(): {
  setNameHandler: (name: string) => void;
} {
  const navigate = useNavigate();
  const dispatch = useAppDispatch();
  const user = useAppSelector((store) => store.user);

  const setNameHandler = (name: string): void => {

    if (name.length < 10 && name !== '') {
      dispatch(setUserName(name));
      dispatch(setCreator(user));
      navigate('/themes');
    } else {
      alert('Введите корректное имя');
    }
  };

  return { setNameHandler };
}
